package com.yu.waterstation.system.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yu.waterstation.common.common.enums.OrderStatus;
import com.yu.waterstation.common.entity.app.TaskLog;
import com.yu.waterstation.common.entity.app.TicketOrder;
import com.yu.waterstation.system.modules.app.mapper.TicketOrderMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class TicketOrderService {

    @Resource
    private TicketOrderMapper ticketOrderMapper;

    // 分页查询
    public Page<TicketOrder> selectPage(int pageNo, int pageSize) {
        return ticketOrderMapper.selectPage(new Page<>(pageNo, pageSize), null);
    }

    // 所有处理中的订单
    public List<TicketOrder> selectOrderHandling() {
        return ticketOrderMapper.selectList(new QueryWrapper<TicketOrder>().eq("order_status", OrderStatus.ENUM_not_pay.code));
    }

    // 完成订单
    @Transactional(rollbackFor = Exception.class)
    public int finishOrder(Integer orderId, String username) {
        TicketOrder order = ticketOrderMapper.selectById(orderId);
        order.setOrderStatus(OrderStatus.ENUM_has_finished.code);
        order.setOrderHandler(username);
        order.setFinishTime(new Date());
        return ticketOrderMapper.update(order, new QueryWrapper<TicketOrder>()
                .eq("id", order.getId())
                .ne("order_status", OrderStatus.ENUM_has_finished.code));
    }

    // 取消订单
    @Transactional(rollbackFor = Exception.class)
    public int cancelOrder(Integer orderId, String username) {
        TicketOrder order = ticketOrderMapper.selectById(orderId);
        order.setOrderStatus(OrderStatus.ENUM_has_canceled.code);
        order.setOrderHandler(username);
        order.setFinishTime(new Date());
        return ticketOrderMapper.update(order, new QueryWrapper<TicketOrder>()
                .eq("id", order.getId())
                .ne("order_status", OrderStatus.ENUM_has_finished.code));
    }
}
